package com.fly.spring.jdbc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.fly.spring.jdbc.dao.UserDao;
import com.fly.spring.jdbc.vo.User;

@Repository
public class UserDaoImpl implements UserDao
{
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    public void setDataSource(DataSource dataSource)
    {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
    
    public void saveUser(User user)
    {
        this.jdbcTemplate.update("INSERT INTO USER (username, age) VALUES (?, ?)", user.getUsername(), user.getAge());
    }
    
    public List<User> listUser()
    {
        List<User> users = this.jdbcTemplate.query("SELECT username, age FROM USER", new RowMapper<User>()
        {
            public User mapRow(ResultSet rs, int rowNum)
                throws SQLException
            {
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        });
        return users;
    }
    
    public void createUserTable()
    {
        this.jdbcTemplate.execute("CREATE TABLE USER (USERNAME varchar(250),AGE INT)");
    }
}
